package com.mk.admin.service.impl;

import com.mk.admin.mapper.DoctorEvaluateMapper;
import com.mk.admin.model.ResultDto;
import com.mk.admin.model.dto.DoctorEvaluateDto;
import com.mk.admin.model.entity.DoctorEvaluateEntity;
import com.mk.admin.model.query.DoctorEvaluateQuery;
import com.mk.admin.model.vo.DoctorEvaluateVo;
import com.mk.admin.service.DoctorEvaluateService;
import com.mk.admin.util.DateTool;
import com.mk.admin.util.JSONCopy;
import com.mk.admin.util.NumberTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class DoctorEvaluateServiceImpl implements DoctorEvaluateService {
    @Autowired
    private DoctorEvaluateMapper doctorEvaluateMapper;

    @Override
    public ResultDto select(DoctorEvaluateQuery query) {
        DoctorEvaluateEntity entity = JSONCopy.copy(query, DoctorEvaluateEntity.class);
        return ResultDto.dataJson(doctorEvaluateMapper.count(entity), doctorEvaluateMapper.select(entity));
    }

    @Override
    public ResultDto patient(DoctorEvaluateQuery query) {
        DoctorEvaluateEntity entity = JSONCopy.copy(query, DoctorEvaluateEntity.class);
        return ResultDto.data(doctorEvaluateMapper.patientCount(entity), doctorEvaluateMapper.patient(entity));
    }

    @Override
    public ResultDto add(DoctorEvaluateVo doctorEvaluateVo) {
        doctorEvaluateVo.setCreateTime(DateTool.format());
        int f1 = NumberTool.strToInt(doctorEvaluateVo.getFractionInfo());
        int f2 = NumberTool.strToInt(doctorEvaluateVo.getFractionServe());
        int f3 = NumberTool.strToInt(doctorEvaluateVo.getFractionSpeciality());
        int f4 = NumberTool.strToInt(doctorEvaluateVo.getFractionSpeed());
        doctorEvaluateVo.setFraction((f1 + f2 + f3 + f4) / 4 + "");
        doctorEvaluateVo.setCreateTime(DateTool.format());
        DoctorEvaluateEntity entity = new DoctorEvaluateEntity();
        entity.setPayOrderNo(doctorEvaluateVo.getPayOrderNo());
        List<DoctorEvaluateDto> list = doctorEvaluateMapper.select(entity);
        if (list.size() == 0) {
            return ResultDto.data(doctorEvaluateMapper.insert(JSONCopy.copy(doctorEvaluateVo, DoctorEvaluateEntity.class)));
        } else {
            return ResultDto.data(new ArrayList<>());
        }
    }

    @Override
    public ResultDto update(DoctorEvaluateVo doctorEvaluateVo) {
        return ResultDto.data(doctorEvaluateMapper.updateById(JSONCopy.copy(doctorEvaluateVo, DoctorEvaluateEntity.class)));
    }

    @Override
    public ResultDto delete(int id) {
        return ResultDto.data(doctorEvaluateMapper.deleteById(id));
    }


    @Override
    public ResultDto status(int id) {
        return ResultDto.data(doctorEvaluateMapper.status(id));
    }
}
